# encoding: UTF-8

module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def sign_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user=user
  end

  def current_user
    @current_user ||= NhanVien.find_by_remember_token(cookies[:remember_token])
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end

  def deny_access
    store_location
    redirect_to new_user_session_path
  end

  def store_location
    session[:return_to] = request.fullpath
  end

  def clear_stored_location
    session[:return_to] = nil
  end

  def redirect_back_or_to(alternate)
    redirect_to(request.original_fullpath || alternate)
    clear_stored_location
  end


end
